<template>
  <div class="app-container">
    待开发


  </div>
</template>

<script>

import { getList, edit, add, del} from '@/api/source'

export default {
  name: "CallBack",
  filters: {

  },
  data() {
    const validateSource = (rule, value, callback) => {
      if (!value) callback(new Error('请输入名称')) 
      else callback()
    }
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      dialogTitle: '添加来源',
      depts:[],
      formRules: {
        source: [{ required: true, trigger: 'blur', validator: validateSource }],
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList({currentPage: this.currentPage, pageSize: this.pageSize}).then(res=>{
        console.log(res)
        this.list = res.data
        this.total = res.data.length
        this.listLoading = false
      }).catch(err=>{
        console.log("err")
        console.log(err)
        this.listLoading = false
      })
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      this.infoVisible = true
    },
    update(row) {
      this.reset()
      this.dialogTitle = '修改来源'
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
    },
    deleteInfo(scope) {
      console.log(scope.row)
      this.$confirm('确定删除该来源?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(scope.row.id).then(res=>{
          this.list.splice(scope.$index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(err=>{
          console.log(err)
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    add(row) {
      this.reset()
      this.dialogTitle = '添加来源'
      this.formVisible = true
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if(this.form.id) {  // update
            console.log(this.form)
            edit(this.form).then(res=>{
              console.log(res)
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/callBack', name: "CallBack"})
            }).catch(err=>{
              console.log(err)
            })
          } else { // add
            console.log(this.form)
            add(this.form).then(res=>{
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/callBack', name: "CallBack"})
            }).catch(err=>{
              console.log(err)
            })
          }
        }
      })
      
    },
    cancel() {
      this.formVisible = false
    },
    reset(){
      this.form = {}
    }
  }
}
</script>
